package cn.thoughtworks.school.programCenter.repositories;

import cn.thoughtworks.school.programCenter.entities.Program;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface ProgramRepository extends JpaRepository<Program, Long> {

  @Query(value = "select * from program where id in (select programId from userProgram where userProgram.userId=?1)", nativeQuery = true)
  List<Program> getProgramsByUserProgramStudentId(long id);

  @Query(value = "select * from program where id in (select programId from tutorProgram where tutorId=?1)", nativeQuery = true)
  List<Program> getProgramsByUserProgramTutorId(long id);

  Page<Program> findByOrganizationIdAndIdIn(Long userCurrentOrganizationId, List<Long> programIds, Pageable pageable);

  Page findAllByAvailableIsTrue(Pageable pageable);

  Page findByAvailableIsTrueAndIdIn(List<Long> myWorkspaceIds, Pageable pageable);

  Optional<Program> findByOrganizationIdAndIdAndAvailableIsTrue(Long organizationId, Long programId);

  List<Program> findAllByAvailableIsTrue();

  Page<Program> findByAvailableIsTrueAndOrganizationIdAndIdIn(Long currentOrganizationId, List<Long> programIds, Pageable pageable);
  Page<Program> findByAvailableIsTrueAndOrganizationIdAndIsExpiredIsFalseAndIdIn(Long currentOrganizationId, List<Long> programIds, Pageable pageable);

  List<Program> findAllByAvailableIsTrueAndOrganizationIdIn(List<Long> organizationIds);

  Page<Program> findByAvailableIsTrueAndOrganizationId(Long organizationId, Pageable pageable);

  List<Program> findByOrganizationId(Long organizationId);

  List<Program> findByIdIn(List<Long> programIds);

  List<Program> findByIdInOrderByIdDesc(List<Long> programIds);
}
